package org.example.day02;

/**
 * @author zlxad
 * @version 1.0
 * @description: TODO
 * 公司用一个字符串来表示员工的出勤信息：
 *   absent：缺勤
 *   late：迟到
 *   leaveearly：早退
 *   present：正常上班
 *   现需根据员工出勤信息，判断本次是否能获得出勤奖，能获得出勤奖的条件如下：
 *   缺勤不超过一次；没有连续的迟到/早退；任意连续7次考勤，缺勤/迟到/早退不超过3次
 *
 * @date 2024/3/11 15:22
 */
public class test030 {
    /**
     * 检查员工是否能获得出勤奖
     * @param attendanceRecords 员工出勤信息数组
     * @return 如果员工满足获得出勤奖的条件则返回true，否则返回false
     */
    public static boolean canGetAttendanceAward(String[] attendanceRecords) {
        // 定义常量
        final String ABSENT = "absent";
        final String LATE = "late";
        final String LEAVE_EARLY = "leaveearly";
        final String PRESENT = "present";
        final int MAX_ABSENCES = 1; // 缺勤不超过一次
        final int MAX_CONSECUTIVE_LATE_OR_LEAVE_EARLY = 0; // 没有连续的迟到/早退
        final int MAX_ABSENCES_IN_7_DAYS = 3; // 连续7次考勤中，缺勤/迟到/早退不超过3次

        int absentCount = 0; // 记录缺勤次数
        int consecutiveLateOrLeaveEarly = 0; // 记录连续迟到或早退次数
        int consecutiveRecordsCount = 0; // 记录连续考勤记录数

        for (String record : attendanceRecords) {
            if (record.equals(ABSENT)) {
                absentCount++;
                consecutiveLateOrLeaveEarly = 0; // 重置连续迟到/早退计数器
                consecutiveRecordsCount = 0; // 重置连续考勤记录计数器
            } else if (record.equals(LATE) || record.equals(LEAVE_EARLY)) {
                consecutiveLateOrLeaveEarly++;
                absentCount = 0; // 重置缺勤计数器
                consecutiveRecordsCount++;

                if (consecutiveLateOrLeaveEarly > MAX_CONSECUTIVE_LATE_OR_LEAVE_EARLY) {
                    return false; // 连续迟到/早退超过限制，不能获得出勤奖
                }
            } else if (record.equals(PRESENT)) {
                absentCount = 0; // 重置缺勤计数器
                consecutiveLateOrLeaveEarly = 0; // 重置连续迟到/早退计数器
                consecutiveRecordsCount++;

                if (consecutiveRecordsCount >= 7) {
                    // 检查连续7次考勤中的缺勤/迟到/早退次数
                    if (absentCount > MAX_ABSENCES_IN_7_DAYS) {
                        return false; // 超过限制，不能获得出勤奖
                    }
                    consecutiveRecordsCount = 0; // 重置连续考勤记录计数器
                }
            }

            // 检查缺勤次数是否超过限制
            if (absentCount > MAX_ABSENCES) {
                return false; // 超过限制，不能获得出勤奖
            }
        }

        return true; // 所有条件都满足，可以获得出勤奖
    }

    public static void main(String[] args) {
        // 测试用例
        String[] attendanceRecords = {"present", "late", "present", "absent", "present", "present", "leaveearly", "present", "present", "present"};
        boolean canAward = canGetAttendanceAward(attendanceRecords);
        System.out.println("Can the employee get the attendance award? " + canAward);
    }
}
